package cn.xianshu.org.controller;

import cn.xianshu.basic.util.AxiosResult;
import cn.xianshu.basic.util.PageList;
import cn.xianshu.org.domain.SystemsDetail;
import cn.xianshu.org.query.SystemsDetailQuery;
import cn.xianshu.org.service.ISystemsDetailService;
import cn.xianshu.org.service.ISystemsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemDetail")
public class StstemsDetailController {
    /**
     * 注入service层对象
     */
    @Autowired
    private ISystemsDetailService systemsDetailService;


    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public SystemsDetail findOne(@PathVariable("id") Long id){
        return systemsDetailService.findById(id);
    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping
    public List<SystemsDetail> findAll(){
        return systemsDetailService.findAll();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AxiosResult delete(@PathVariable("id") Long id){
        try {
            systemsDetailService.delete(id);
            return AxiosResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统繁忙，请稍后重试。。。。");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AxiosResult Bydelete(@RequestBody List<Long> ids){
        try {
            systemsDetailService.Bydelete(ids);
            return AxiosResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统繁忙，请稍后重试。。。。");
        }
    }

    /**
     * 添加修改
     * @param
     * @return
     */
    @PutMapping
    public AxiosResult addAndUpdate(@RequestBody SystemsDetail systemsDetail){
        try {
            if (systemsDetail.getId()==null){
                systemsDetailService.insert(systemsDetail);
            } else {
                systemsDetailService.update(systemsDetail);
            }
            return AxiosResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统繁忙，请稍后重试。。。。");
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    public PageList<SystemsDetail> queryPage(@RequestBody SystemsDetailQuery query){
        return systemsDetailService.pageQuery(query);
    }

    /**
     *查询部门数
     * @return
     */
    @GetMapping("/tree")
    public List<SystemsDetail> queryTree(){
        return systemsDetailService.queryTree();
    }
}
